#include <bits/stdc++.h>
using namespace std;
using uint=unsigned int;
using ll=long long;
using ull=unsigned long long;
using i128=__int128;
const int N=1e6+5;

void solve(){
    int n;
    cin>>n;
    vector<pair<int,int>> a(n);
    for(int i=0;i<n;i++){
        cin>>a[i].first>>a[i].second;
    }
    sort(a.begin(),a.end(),[](const pair<int,int> &x,const pair<int,int> &y){
        return x.second<y.second;
    });
    priority_queue<int> q;
    int time=0;
    int ans=0;
    for(int i=0;i<n;i++){
        if(time+a[i].first<=a[i].second){
            ans++;
            time+=a[i].first;
            q.push(a[i].first);
        }
        else if(!q.empty()&&q.top()>a[i].first){
            time-=q.top();
            q.pop();
            time+=a[i].first;
            q.push(a[i].first);
        }
    }
    cout<<ans<<endl;
    return;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    solve();

    return 0;
}